import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import net.miginfocom.swing.MigLayout;

public class Userverwaltung extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField txtVorname;
	private JTextField txtNachname;
	private JTextField txtBenutzername;
	private JTextField txtPassword;
	private String vorname, nachname, benutzername, password;
	private int rechte = 1; // 1=Benutzer  2=Manager  3=Administrator
	private JTable jTable1 = new JTable(30, 5);
    private DefaultTableModel jTable1Model = (DefaultTableModel) jTable1.getModel();
    private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);
	public boolean isCellEditable(int row, int col){
        return false;}
    private Database db;
    private User user;
    private JComboBox comboBox;
    
	public Userverwaltung(Database db, User userObj) throws SQLException {
		jTable1.setFont(new Font("Verdana", Font.PLAIN, 12));
		this.db=db;
		this.user=userObj;
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 760, 620);
	    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	    int x = (d.width - getSize().width) / 2;
	    int y = (d.height - getSize().height) / 2;
	    setLocation(x, y);
	    

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenuItem mntmKinoverwaltung = new JMenuItem("Kinoverwaltung");
		mntmKinoverwaltung.setFont(new Font("Verdana", Font.PLAIN, 14));
		menuBar.add(mntmKinoverwaltung);
		mntmKinoverwaltung.addActionListener(this);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(51, 153, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel lblUserverwaltung = new JLabel("Take A Seat - Userverwaltung");
		lblUserverwaltung.setBackground(SystemColor.window);
		lblUserverwaltung.setForeground(new Color(255, 255, 255));
		lblUserverwaltung.setFont(new Font("Verdana", Font.BOLD, 36));

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("Verdana", Font.PLAIN, 14));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblUserverwaltung))
						.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 738, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblUserverwaltung)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 494, Short.MAX_VALUE)
					.addContainerGap())
		);

		JPanel user_anlegen_panel = new JPanel();
		tabbedPane.addTab("User anlegen", null, user_anlegen_panel, null);

		JLabel label = new JLabel("Rolle");
		label.setFont(new Font("Verdana", Font.BOLD, 14));

		comboBox = new JComboBox();
		// comboBox.setModel(new DefaultComboBoxModel(new String[] {"Admin",
		// "Superadmin"}));
		comboBox.addItem("Admin");
		comboBox.addItem("Manager");
		comboBox.addItem("Kassierer");
		comboBox.setFont(new Font("Verdana", Font.BOLD, 12));
		comboBox.addItemListener(new ComboBoxItemListener());

		JLabel label_1 = new JLabel("Benutzername");
		label_1.setFont(new Font("Verdana", Font.BOLD, 14));
		// label_1.setBorder(BorderFactory.createLineBorder(Color.black, 1));

		JLabel label_2 = new JLabel("Nachname");
		label_2.setFont(new Font("Verdana", Font.BOLD, 14));

		JLabel label_3 = new JLabel("Vorname");
		label_3.setFont(new Font("Verdana", Font.BOLD, 14));

		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Verdana", Font.BOLD, 14));

		JButton btnUserAnlegen = new JButton("User anlegen");
		btnUserAnlegen.addActionListener(this);
		btnUserAnlegen.setFont(new Font("Verdana", Font.PLAIN, 14));
		

		txtVorname = new JTextField();
		txtVorname.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtVorname.setColumns(10);

		txtNachname = new JTextField();
		txtNachname.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtNachname.setColumns(10);

		txtBenutzername = new JTextField();
		txtBenutzername.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtBenutzername.setColumns(10);

		txtPassword = new JTextField();
		txtPassword.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtPassword.setColumns(10);
		GroupLayout gl_user_anlegen_panel = new GroupLayout(user_anlegen_panel);
		gl_user_anlegen_panel.setHorizontalGroup(
			gl_user_anlegen_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_user_anlegen_panel.createSequentialGroup()
					.addGap(43)
					.addGroup(gl_user_anlegen_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPassword))
					.addGap(28)
					.addGroup(gl_user_anlegen_panel.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(btnUserAnlegen, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(comboBox, Alignment.LEADING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(txtPassword, Alignment.LEADING)
						.addComponent(txtBenutzername, Alignment.LEADING)
						.addComponent(txtNachname, Alignment.LEADING)
						.addComponent(txtVorname, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
					.addGap(390))
		);
		gl_user_anlegen_panel.setVerticalGroup(
			gl_user_anlegen_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_user_anlegen_panel.createSequentialGroup()
					.addGap(55)
					.addGroup(gl_user_anlegen_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtVorname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_user_anlegen_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtNachname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(24)
					.addGroup(gl_user_anlegen_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtBenutzername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(24)
					.addGroup(gl_user_anlegen_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_user_anlegen_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addComponent(btnUserAnlegen, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
					.addGap(216))
		);
		user_anlegen_panel.setLayout(gl_user_anlegen_panel);

		JPanel user_aendern_panel = new JPanel();
		tabbedPane.addTab("User \u00E4ndern", null, user_aendern_panel, null);
		
		jTable1.getColumnModel().getColumn(0).setHeaderValue("ID");
		jTable1.getColumnModel().getColumn(1).setHeaderValue("Vorname");
		jTable1.getColumnModel().getColumn(2).setHeaderValue("Nachname");
		jTable1.getColumnModel().getColumn(3).setHeaderValue("Benutzername");
		jTable1.getColumnModel().getColumn(4).setHeaderValue("Rechte");
		
		 ResultSet rs = db.getData("SELECT UserID,Vorname,Nachname,Benutzername,Rechte FROM User");
		    
		    int row=0;
		    	    
		    while(rs.next()){
		    	    jTable1.setValueAt(rs.getString(1),row,0);
		    	    jTable1.setValueAt(rs.getString(2),row,1);
		    	    jTable1.setValueAt(rs.getString(3),row,2);
		    	    jTable1.setValueAt(rs.getString(4),row,3);
		    	    String temp=null;
		    	    switch(Integer.parseInt(rs.getString(5))){
		    	    case 0: temp="Gast";break;
		    	    case 1: temp="Kassierer";break;
		    	    case 2: temp="Manager";break;
		    	    case 3: temp="Admin";break;
		    	    default:temp="Rolle unbekannt";break;
		    	    }
		    	    jTable1.setValueAt(temp,row,4);
		       
		    	    row++;
		    }
		
		JButton btnnderungenbernehemen = new JButton("\u00C4ndern");
		btnnderungenbernehemen.addActionListener(this);
		btnnderungenbernehemen.setFont(new Font("Verdana", Font.PLAIN, 14));
		contentPane.setLayout(gl_contentPane);
		
		JButton btnZuruecksetzen = new JButton("User freischalten");
		btnZuruecksetzen.setFont(new Font("Verdana", Font.PLAIN, 14));
		GroupLayout gl_user_aendern_panel = new GroupLayout(user_aendern_panel);
		gl_user_aendern_panel.setHorizontalGroup(
			gl_user_aendern_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_user_aendern_panel.createSequentialGroup()
					.addGap(7)
					.addComponent(jTable1ScrollPane, GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_user_aendern_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(btnnderungenbernehemen)
						.addComponent(btnZuruecksetzen, GroupLayout.PREFERRED_SIZE, 216, GroupLayout.PREFERRED_SIZE))
					.addGap(12))
		);
		gl_user_aendern_panel.setVerticalGroup(
			gl_user_aendern_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_user_aendern_panel.createSequentialGroup()
					.addGroup(gl_user_aendern_panel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_user_aendern_panel.createSequentialGroup()
							.addContainerGap()
							.addComponent(btnZuruecksetzen)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnnderungenbernehemen))
						.addGroup(Alignment.LEADING, gl_user_aendern_panel.createSequentialGroup()
							.addGap(11)
							.addComponent(jTable1ScrollPane)))
					.addContainerGap(14, Short.MAX_VALUE))
		);
		user_aendern_panel.setLayout(gl_user_aendern_panel);
		contentPane.setLayout(gl_contentPane);
		btnZuruecksetzen.addActionListener(this);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand()=="\u00C4ndern"){
			System.out.println("ActionPerformed angelangt Event korrekt");
			int id = Integer.parseInt((String) jTable1.getValueAt(jTable1.getSelectedRow(), 0));
			try {
				new UserChange(id, db, user);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			dispose();
		}
		
		if (e.getActionCommand() == "Kinoverwaltung") {
			dispose();
			try {
				new Kinoverwaltung(db, user);//.main(null);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if(e.getActionCommand()=="User freischalten"){
			try {
				db.updateFehlversuche((String) jTable1.getValueAt(jTable1.getSelectedRow(), 3), 0);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if (e.getActionCommand() == "User anlegen") {
			vorname = txtVorname.getText();
			nachname = txtNachname.getText();
			benutzername = txtBenutzername.getText();
			password = txtPassword.getText();
			
			String temp = comboBox.getSelectedItem().toString();
			if(temp.equals("Kassierer"))
				rechte=1;
			else if (temp.equals("Manager"))
				rechte=2;
			else if (temp.equals("Admin"))
				rechte=3;
			else rechte=0;
			
System.out.println(vorname+"\n"+nachname+"\n"+benutzername+"\n"+password+"\n"+rechte);

		User user = new User(benutzername, vorname, nachname, password, rechte);
			try {
				db.setUser(user);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}   
		}
	
	}
	class ComboBoxItemListener implements ItemListener {
		public void itemStateChanged(ItemEvent e) {
			JComboBox cb = (JComboBox) e.getSource();
		 String rolle=cb.getSelectedItem().toString();
		}
	}
	
}
